package com.videoanswer.van.web.client.controller;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.videoanswer.van.service.user.UserService;
import com.videoanswer.van.service.user.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;
	
	@RequestMapping(value = { "/login" }, method = { RequestMethod.POST })
	public ModelAndView postLogin(HttpServletRequest request, HttpServletResponse response, 
			HttpSession session) throws Exception{
			
		Map<String, Object> map = new HashMap<String, Object>();
		
		String email = request.getParameter("username");
		String password = request.getParameter("password");
		String studentOrTeacher = request.getParameter("studentOrTeacher");

        UserModel userModel = new UserModel();
		boolean isTeacher = false;
		
		
		if(null != studentOrTeacher){
			if(studentOrTeacher.equals("student")){
                userModel = userService.login(email, password);
				
			}else if(studentOrTeacher.equals("teacher")){
				isTeacher = true;
				
			}else{
				map.put("isUnitPage", true);
				map.put("hasMainNav", false);
				map.put("loginError", true);
				return new ModelAndView("/studentLogin", map);
			}
			
		}else{
			map.put("isUnitPage", true);
			map.put("hasMainNav", false);
			map.put("loginError", true);
			return new ModelAndView("/studentLogin", map);
		}
		
		
		if(null != userModel && null != userModel.getEmail() && !userModel.getEmail().equals("")){
			map.put("username", userModel.getName());
			map.put("isStudent", true);
			return new ModelAndView("/overview", map);
			
		}else if(isTeacher){
			map.put("isTeacher", true);
			map.put("username", "teacherName");
			map.put("hasMainNav", false);
			return new ModelAndView("/teacherMainPage", map);
			
		}else{
			map.put("isUnitPage", true);
			map.put("loginError", true);
			map.put("hasMainNav", false);
			return new ModelAndView("/studentLogin", map);
		}
	}
	
	@RequestMapping(value = { "/studentLogin" }, method = { RequestMethod.GET })
	public ModelAndView getStudentLogin(HttpServletRequest request, HttpServletResponse response, 
			HttpSession session)throws Exception{
			
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isUnitPage", true);
		map.put("hasMainNav", false);
		
		return new ModelAndView("/studentLogin", map);
	}
	
	@RequestMapping(value = { "/teacherLogin" }, method = { RequestMethod.GET })
	public ModelAndView getTeacherLogin(HttpServletRequest request, HttpServletResponse response, 
			HttpSession session)throws Exception{
			
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isUnitPage", true);
		map.put("hasMainNav", false);
		
		return new ModelAndView("/teacherLogin", map);
	}

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}




